package com.hlf.modules.api.service.impl;

import com.hlf.common.constants.Constants;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.modules.api.entity.CartEntity;
import com.hlf.modules.api.mapper.CartEntityMapper;
import com.hlf.modules.api.service.CartService;
import com.hlf.modules.h5.interceptor.AuthorizationInterceptor;
import com.hlf.modules.merchant.goods.entity.Goods;
import com.hlf.modules.merchant.goods.service.GoodsService;
import com.hlf.modules.merchant.table.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 购物车实现类
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartEntityMapper cartEntityMapper;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TableService tableService;

    /**
     * 查购物车信息
     *
     * @return
     */
    @Override
    public CartEntity queryByGoodsIdAndTableId(Long goodsId, Long tableId) {
        return cartEntityMapper.selectByGoodsIdAndTableId(goodsId, tableId);
    }

    /**
     * 修改
     *
     * @param cartEntity
     */
    @Override
    public void updateByPrimaryKeySelective(CartEntity cartEntity) {
        cartEntityMapper.updateByPrimaryKeySelective(cartEntity);
    }


    /**
     * 新增
     *
     * @param cartEntity
     */
    @Override
    public void insertSelective(CartEntity cartEntity) {
        cartEntityMapper.insertSelective(cartEntity);
    }

    /**
     * 餐桌列表
     *
     * @param tableId
     * @return
     */
    @Override
    public List<CartEntity> queryCartListByTableId(Long tableId) {
        return cartEntityMapper.selectListByTableId(tableId);
    }

    /**
     * 清空购物车
     *
     * @param tableId
     */
    @Override
    public void deleteCartByTableId(Long tableId) {
        cartEntityMapper.deleteByTableId(tableId);
    }

    /**
     * 是否存在商品
     *
     * @param tableId
     * @return
     */
    @Override
    public Boolean cartExistGood(Long tableId) {
        Integer count = cartEntityMapper.selectCountByTableId(tableId);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 添加购物车
     *
     * @param goodsId
     * @param goodsNum
     * @param tableId
     * @param request
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCart(String goodsId, String goodsNum, String tableId, HttpServletRequest request) {
        //当前登陆用户id
        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        Long goodsIdL = Long.parseLong(goodsId);
        Long tableIdL = Long.parseLong(tableId);
        CartEntity cartEntity = queryByGoodsIdAndTableId(goodsIdL, tableIdL);
        Goods goods = goodsService.queryObject(goodsIdL);
        if (goods.getRemainQty() <= Integer.valueOf(goodsNum)) {
            throw new BusinessException("抱歉,"+goods.getName()+"库存不足"+goodsNum+"个!");
        }
        if (!Constants.GOODS_STATUS_SHELVE.equals(goods.getStatus())) {
            throw new BusinessException("抱歉,此商品未上架!");
        }
        if (cartEntity != null) {
            //购物车中已存在,更新商品数
            Integer goodsNumber = Integer.parseInt(goodsNum);
            cartEntity.setGoodsNum(goodsNumber);
            cartEntity.setLastUpdateBy(userId);
            updateByPrimaryKeySelective(cartEntity);
        } else {
            // 购物车没有,则添加
            cartEntity = new CartEntity();
            cartEntity.setId(IdWorkerFactory.nextId());
            cartEntity.setTableId(tableIdL);
            cartEntity.setGoodsTypeId(goods.getGoodsType());
            cartEntity.setGoodsNum(Integer.parseInt(goodsNum));
            cartEntity.setGoodsId(goodsIdL);
            cartEntity.setGoodsName(goods.getName());
            cartEntity.setGoodsPrice(goods.getSellPrice());
            cartEntity.setForeignId(userId);
            cartEntity.setLastUpdateBy(userId);

            insertSelective(cartEntity);
        }
    }

    /**
     * 批量添加
     *
     * @param goodsInfo
     * @param tableId
     * @param request
     */
    @Override
    @Transactional
    public void batchCreateCart(List<String> goodsInfo, String tableId, HttpServletRequest request) {
        int num = 0;
        int index = 0;
        String goodsId;
        String goodsNum;

        if (goodsInfo.size() > 0) {
            for (String s : goodsInfo) {
                for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) == '/') {
                        index = i;
                        num++;
                    }
                }
                if (num != 1) {
                    throw new BusinessException("参数不正确!");
                }
                num = 0;
                goodsId = s.substring(0, index);
                goodsNum = s.substring(index + 1);
                createCart(goodsId, goodsNum, tableId, request);

            }
        }

    }

}
